<?php

/**
 * Created by PhpStorm.
 * role: shwy
 * Date: 2017/5/18
 * Time: 15:55
 * 权限配置管理
 */
class Role extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('role_model');
        $this->load->model('permission_model');
		
		$username = $this->session->userdata('username');
		$userid   = $this->session->userdata('userid');
		
		if (!$username || !$userid) {
			redirect('admin/login/index');
		}
		
//      if(!isset($_COOKIE['nickname']) && !isset($_COOKIE['admin']) && empty($_COOKIE['admin'])){
//          echo __alert('没有权限!');
//      }
    }

    // 权限组
    public function index()
    {
        // 只有管理员权限才能访问
        if ($this->session->userdata('username') !== 'admin') {
        	die('抱歉你没权限访问此页面');
			redirect('home/index');
        }
//      if($_COOKIE['username'] !== 'admin'){
//          die('抱歉你没权限访问此页面');
//      }

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (isset($_GET['search']) && !empty($_GET['search'])) {
            $searchArg = [
                'field' => 'name',
                'arg' => $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->role_model->search('*', 0, $searchArg, $page_num, $start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum' => $page_num //每页显示多少条
            ];

            $this->load->view('admin/role/list', $viewData);
        } else {
            $data = $this->role_model->page('role', 0, $page_num, $start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum' => $page_num //每页显示多少条

            ];

            $this->load->view('admin/role/list', $viewData);
        }


    }

    // 角色新增接口
    public function add()
    {

        $data = [
            'name' => $this->input->post('name'),
            'createTime' => time(),
        ];

        //是否会有添加失败的情况?

        if (__checkData($data)) {
            $resultData = $this->role_model->add($data);

            if ($resultData) {
                redirect('/admin/role/index?page=1');
            }
        } else {
            echo __alert('参数错误');
        }
    }


    // 编辑角色名字
    public function edit()
    {
        //用户id
        $where = [
            'id' => $this->input->post('id')
        ];

        $data = [
            'name' => $this->input->post('name'),
        ];

        if (__checkData($data)) {
            //是否会有添加失败的情况?
            $resultData = $this->role_model->edit($data, $where);

            if ($resultData) {
                redirect('/admin/role/index?page=1');
            }
        } else {
            echo __alert('参数错误');

        }


    }

    // 批量删除
    public function remove()
    {

        $postData = $this->input->post();

        if ($postData) {
            if (!$postData) {
                redirect('/admin/role/index?page=1');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v) {
                $where = [
                    'id' => $v
                ];

                $this->role_model->remove($where);
            }
            redirect('/admin/role/index?page=1');

        } else {
            $data = [
                'id' => $this->input->get('id'),
            ];

            //是否会有添加失败的情况?
            $resultData = $this->role_model->remove($data);
            if ($resultData) {
                redirect('/admin/role/index?page=1');
            }
        }

    }


    // 角色权限设置页面
    public function setview()
    {
        // 权限配置页面数据动态化
        $arg = [
            'id' => $this->input->get('id')
        ];

        $data['list'] = $this->role_model->getByFieldQueryData('*', $arg, 0);

        // 如果不存在直接跳转到404页面
        if (!$data['list']) {
            redirect('/home/error');
        }

        $permission = [];
        $arg2 = [
            'pid' => 0
        ];
        $permission = $this->permission_model->getByFieldQueryData('*', $arg2, 1);  //主权限

        $arg3 = [
            'pid !=' => 0
        ];
        $childData = $this->permission_model->getByFieldQueryData('*', $arg3, 1);  //子权限

        $ids = explode(',', $data['list']['permission_id']);
        foreach ($permission as $k4 => $v4) {
            foreach ($ids as $k3 => $v3) {
                if ($v3 == $v4['id']) {
                    $permission[$k4]['isCheck'] = 1;
                    break;
                } else {
                    $permission[$k4]['isCheck'] = 0;
                }
            }
        }

        foreach ($childData as $k6 => $v6) {
            foreach ($ids as $v5) {
                if ($v5 == $v6['id']) {
                    $childData[$k6]['isCheck'] = 1;
                    break;
                } else {
                    $childData[$k6]['isCheck'] = 0;
                }
            }
        }

        foreach ($permission as $k => $v) {
            foreach ($childData as $k2 => $v2) {
                if ($v['id'] == $v2['pid']) {
                    $permission[$k]['child'][] = $v2;
                }
            }
        }
        //Todo 暂时写死,后期待优化
        $key = [
            '审核管理',
            '财务审核',
            '预警管理',
            '供应商管理',
            '项目管理',
            '采购管理',
            '采购管理',
            '采购管理',
            '采购管理',
            '采购管理',
            '商品管理',
            '商品管理',
            '用户管理',
            '用户管理',
            '库存管理',
            '库存管理',
            '库存管理',
            '统计分析管理',
            '统计分析管理',
            '招投标管理',
            '招投标管理',
            '招投标管理',
            '招投标管理',
        ];

        foreach ($permission as $k7=>$v7){

            $permission[$k7]['title'] =  $key[$k7];
        }

        $data['permission'] = $permission;

        $this->load->view('/admin/role/edit', $data);


    }

	// 具体权限设置功能接口
    public function permission()
    {
        // 用户id
        $where = [
            'id' => $_POST['id']  //要修改的id
        ];
        $data = [
            'permission_id' => $_POST['permission'],  //权限组合id 1,2,3,4
            'createTime' => time()
        ];


        //是否会有添加失败的情况?
        $this->role_model->edit($data, $where);

        $arr = [
            'success' => 1
        ];
        echo json_encode($arr, JSON_UNESCAPED_UNICODE);

    }

	// 权限组列表 和搜索一起  接口
    public function roleList()
    {
        $page_num = 4;   //每页显示多少条
        $page = $_POST['page']; //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (isset($_POST['search'])) {
            $searchArg = [
                'field' => 'name',
                'arg' => $this->input->post('search'),
                'side' => 'both'
            ];

            $data = $this->role_model->search('*', 0, $searchArg, $page_num, $start_position);
            $viewData = [
                '0' => $data[0],  //这里得到从数据库中的当页数据
                'total_num' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum' => $page_num, //每页显示多少条
                'this_page' => $page
            ];
            echo json_encode($viewData, JSON_UNESCAPED_UNICODE);

        } else {
            $role = $this->role_model->page('role', 0, $page_num, $start_position); //limit查询的数据
            $role['pageNum'] = $page_num;
            $role['this_page'] = $page;
            echo json_encode($role, JSON_UNESCAPED_UNICODE);
        }
    }
}